package leetcode;

import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Liu Yue
 * @Descripition: 罗马数字转整数
 * @Date; Create in 2020/2/16 15:46
 **/
public class RomanTointeger {

    //硬解析
    static int romanToInt(String s) {

        int rs = 0;

        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {

            switch (chars[i]) {
                case 'I':
                    rs = rs + 1;
                    break;
                case 'V':
                    rs = rs + 5;
                    break;
                case 'X':
                    rs = rs + 10;
                    break;
                case 'L':
                    rs = rs + 50;
                    break;
                case 'C':
                    rs = rs + 100;
                    break;
                case 'D':
                    rs = rs + 500;
                    break;
                case 'M':
                    rs = rs + 1000;
                    break;
                default:
                    System.out.println("default");
                    break;
            }
            if (i != 0) {
                if (((chars[i] == 'V') || (chars[i] == 'X')) && (chars[i] == 'I')) {
                    rs = rs - 1 * 2;
                }
                if (((chars[i] == 'L') || (chars[i] == 'C')) && (chars[i] == 'X')) {
                    rs = rs - 10 * 2;
                }
                if (((chars[i] == 'D') || (chars[i] == 'M')) && (chars[i] == 'C')) {
                    rs = rs - 100 * 2;
                }
            }
        }
        System.out.println(rs);
        return rs;

    }

    //硬解析
    static int romanToInt2(String s) {
       /* Map<String, Integer> map = new HashMap<>();
        map.put("I", 1);
        map.put("IV", 4);
        map.put("V", 5);
        map.put("IX", 9);
        map.put("X", 10);
        map.put("XL", 40);
        map.put("L", 50);
        map.put("XC", 90);
        map.put("C", 100);
        map.put("CD", 400);
        map.put("D", 500);
        map.put("CM", 900);
        map.put("M", 1000);

        int ans = 0;
        for (int i = 0; i < s.length(); ) {
            if (i + 1 < s.length() && map.containsKey(s.substring(i, i + 2))) {
                ans += map.get(s.substring(i, i + 2));
                i += 2;
            } else {
                ans += map.get(s.substring(i, i + 1));
                i++;
            }
        }
        System.out.println(ans);*/
        return 0;
    }

    //左减右加方法
    static int romanToInt3(String s) {
       /* Map<String, Integer> map = new HashMap<>();
        map.put("I", 1);
        map.put("V", 5);
        map.put("X", 10);
        map.put("L", 50);
        map.put("C", 100);
        map.put("D", 500);
        map.put("M", 1000);*/

        /*int ans = 0;
        char[] chars = s.toCharArray();
        for (int i = 0, len = chars.length; i < len; i++) {
            int newans = 0;
            char left = chars[i];
            char right = 0;
            Integer li = map.get(String.valueOf(left));
            if (i < len - 1) {
                right = chars[i + 1];
                Integer ri = map.get(String.valueOf(right));
                newans = li < ri ? 0 - li : li;
            }
            if (i == len - 1) {
                newans = li;
            }
            ans += newans;
        }*/

        return 0;
    }

    public static void main(String[] args) {
        RomanTointeger.romanToInt3("XXVII");
    }
}
